package com.polytech.ihm;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.polytech.beans.Booking;
import com.polytech.beans.Car;
import com.polytech.beans.Client;
import com.polytech.business.ReservationManager;
import com.polytech.utils.BusinessFactory;
import com.toedter.calendar.JCalendar;



public class FrameBooking extends JFrame implements WindowListener ,ActionListener{
	private LabeledCalender bookingDate=new LabeledCalender("Booking :");
	private LabeledCalender returnDate=new LabeledCalender("return :");
	private PanelPicture panelPicture=new PanelPicture(400, 400, PanelPicture.DEFINESIZE);
	private JPanel pan=new JPanel(new FlowLayout(FlowLayout.LEFT));
	private JPanel pan1=new JPanel();
	private JButton book=new JButton("Book");
	private FrameClient frameClient;
	private Client client;
	private Car car;
	
	
	public FrameBooking() {
		panelPicture.setBorder(BorderFactory.createBevelBorder(0));
		pan1.setLayout(new BoxLayout(pan1, BoxLayout.Y_AXIS));
		pan1.add(bookingDate);
		pan1.add(returnDate);
		pan1.add(book);
		
		book.addActionListener(this);
		pan.add(panelPicture);
		pan.add(pan1);
		this.add(pan);
		this.pack();
		
	}
	
	public void show(Car car,Client client, FrameClient frameClient){
		this.car=car;
		this.client=client;
		this.frameClient=frameClient;
		panelPicture.setPicture(car.getPicture());
		this.toFront();
		this.setVisible(true);
		
	}
	
	public void hide(){
		super.hide();
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		hide();
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Date dateB=bookingDate.getDate();
		Date dateR=returnDate.getDate();
		
		if(dateB.compareTo(dateR)==1){
			JOptionPane.showMessageDialog(this, "ERROR : return date is befor the booking date");
		}
		else{
			
			ReservationManager reservationManager=BusinessFactory.getReservationManager();
			reservationManager.create(new Booking(dateB, dateR, client, car));
			JOptionPane.showMessageDialog(this, "Operation Done ");
			frameClient.updateListAds();
			this.hide();
		}
		
		
		
	}
}
